package zhoufu.sz.sanxiao_check.presents;

import android.text.TextUtils;
import javax.inject.Inject;
import rx.Subscription;
import zhoufu.sz.sanxiao_check.models.User;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.net.RequestRepository;
import zhoufu.sz.sanxiao_check.ui.contract.LoginContract;
import zhoufu.sz.sanxiao_check.utils.SharedPreferencesUtil;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/19
 */
public class LoginPresenter implements LoginContract.Presenter
{

    private       LoginContract.View    loginV;
    private final RequestRepository     _requestHelper;
    private       SharedPreferencesUtil _sharedPreferencesUtil;
    private       Subscription          _loginSubscription;

    @Inject
    public LoginPresenter(RequestRepository requestRepository)
    {

        _requestHelper = requestRepository;
    }

    public void bindV(LoginContract.View view)
    {
        loginV = view;
    }

    /**
     * 获取User中存储的数据
     */
    @Override
    public void onStart()
    {
        Object entity = _sharedPreferencesUtil.readEntity(User.SAVE_USER, User.class);
        if ( entity != null && entity instanceof User )
        {
            User user = (User) entity;

            loginV.setUser(user);
        } else
        {
            loginV.setUser(null);
        }
        upgradeApp();
    }

    private void upgradeApp()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onDestroy()
    {
        _requestHelper.onDestory();
    }

    public void login(final User user)
    {
        if ( isEmpty(user) )
        {
            loginV.checkEmpty();
            return;
        }
        _loginSubscription = _requestHelper
                .login(user, uuid(), versionName(), new RequestHelper.RequestCallBack<User>()
                {
                    @Override
                    public void onPullStart()
                    {
                        loginV.showProgress();
                    }

                    @Override
                    public void onPullFailud(Throwable throwable)
                    {
                        loginV.hideProgress();
                        loginV.pullFaild();
                    }

                    @Override
                    public void onPullEnd()
                    {
                        loginV.hideProgress();
                        loginV.pullEnd();
                    }

                    @Override
                    public void onPullSuccessed(User s)
                    {
                        loginV.hideProgress();
                        if ( s.isSuccess() )
                        {
                            User loginUser = s.getData().get(0);
                            loginUser.setName(user.getName());
                            loginUser.setPassword(user.getPassword());
                            User.putInstance(loginUser);
                            _sharedPreferencesUtil.saveEntity(loginUser, User.SAVE_USER);
                            loginV.loginSuccessed();
                        } else
                        {
                            loginV.loginFaild(s.getErrMsg());
                            loginV.showToast(s.getErrMsg());
                        }
                    }
                });
    }

    private String uuid()
    {
        return loginV.getUUid();
    }

    private String versionName()
    {
        return loginV.getVersionName();
    }

    private boolean isEmpty(User user)
    {
        return TextUtils.isEmpty(user.getName()) || TextUtils.isEmpty(user.getPassword());
    }

    @Inject
    public void setSpUtil(SharedPreferencesUtil sharedPreferencesUtil)
    {
        _sharedPreferencesUtil = sharedPreferencesUtil;
    }

    public void cancelLogin()
    {
        if ( null != _loginSubscription )
        {
            _requestHelper.unSub(_loginSubscription);
        }
    }
}
